<?php

require_once 'opendb.php';
require_once 'course.php';
require_once 'assignmentManager.php';

class courseManager
{
    public static function addCourse($course)
    {
        //$course = mysql_real_escape_string($course);
        $userID = $course->getUserID();
        $professor = $course->getProfessorID();
        $startDate = $course->getStartDate();
        $endDate = $course->getEndDate();
        $name = $course->getName();
        $building = $course->getBuilding();
        $room = $course->getRoom();
        $insert = mysql_query("INSERT INTO courses VALUES('$userID', NULL, '$name', '$building', '$room', '$professor', '$startDate', '$endDate')") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
        {
            $select = mysql_query("SELECT LAST_INSERT_ID()");
            $result = mysql_num_rows($select);
            $courseID = $result['CourseID'];
            $insert = mysql_query("INSERT INTO course_gradingscale VALUES('$courseID', '0', 'default', '0')");
            echo "The course has been added successfully.";
        } else
            echo "The course has not been added successfully.";


    }

    public static function deleteCourse($course)
    {
        $course = mysql_real_escape_string($course);
        $id = $course->getID();
        $delete = mysql_query("DELETE FROM courses WHERE CourseID = '$id' LIMIT 1") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The course has been successfully deleted.";
        else
            echo "The course has not been successfully deleted.";
    }

    public static function editCourse($course)
    {
        $course = mysql_real_escape_string($course);
        $id = $course->getID();
        $userID = $course->getUserID();
        $professor = $course->getProfessorID();
        $startDate = $course->getStartDate();
        $endDate = $course->getEndDate();
        $name = $course->getName();
        $building = $course->getBuilding();
        $room = $course->getRoom();
        $update = mysql_query("UPDATE courses SET UserID = '$userID', Name = '$name', Building = '$building', Room = '$room', ProfessorID = '$professor', StartDate = '$startDate', EndDate = '$endDate' WHERE CourseID = '$id' LIMIT 1") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The course has been successfully updated.";
        else
            echo "The course has not been successfully updated.";
    }

    public static function getCourse($courseID)
    {
        $courseID = mysql_real_escape_string($courseID);
        $select = mysql_query("SELECT * FROM courses WHERE CourseID = '$courseID'") or die(mysql_error());
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $courseInfo = mysql_fetch_array($select) or die("can't seem to form an array of the course");
            $course = new Course();
            $course->setUserID($courseInfo['UserID']);
            $course->setName($courseInfo['Name']);
            $course->setID($courseInfo['CourseID']);
            $course->setStartDate($courseInfo['StartDate']);
            $course->setEndDate($courseInfo['EndDate']);
            $course->setAssignments(assignmentManager::getAllAssignments($courseInfo['UserID']));
            $course->setBuilding($courseInfo['Building']);
            $course->setRoom($courseInfo['Room']);
            $course->setProfessorID($courseInfo['ProfessorID']);
            return $course;
        } else
        {
            echo "There is no course with that ID.";
        }
    }

    public static function getAllCourses($userID)
    {
        $userID = mysql_real_escape_string($userID);
        $select = mysql_query("SELECT * FROM courses WHERE UserID = '$userID'") or die(mysql_error());
        $courseArray = array();
        while($row = mysql_fetch_array($select))
        {
            $course = new Course();
            $course = courseManager::getCourse($row['CourseID']);
            $courseArray[] = $course;
        }
        return $courseArray;
    }
}

?>
